package validator

import (
	"errors"
	"fmt"
	apis2 "gluenet/pkg/apis"
)

const (
	// must equal to
	_default = apis2.ModuleDefault
	_helm    = apis2.ModuleHelm

	_application = apis2.KindApplication
	_strategy    = apis2.KindStrategy
)

const (
	// validate for default config
	ResourceConfigRegexp            = "^(resource\\." + _default + "\\.config\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
	ResourceConfigApplicationRegexp = "^(resource\\." + _default + "\\.config\\." + _application + "\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
	ResourceConfigStrategyRegexp    = "^(resource\\." + _default + "\\.config\\." + _strategy + "\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"

	ResourceInstanceRegexp            = "^(resource\\." + _default + "\\.instance\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
	ResourceInstanceApplicationRegexp = "^(resource\\." + _default + "\\.instance\\." + _application + "\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
	ResourceInstanceStrategyRegexp    = "^(resource\\." + _default + "\\.instance\\." + _strategy + "\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
)

const (
	// validate for helm config
	ResourceHelmConfigRegexp            = "^(resource\\." + _helm + "\\.config\\.)(([a-z]|[0-9]|[-_/])+\\.)*(([a-z]|[0-9]|[-_/])+)$"
	ResourceHelmConfigApplicationRegexp = "^(resource\\." + _helm + "\\.config\\." + _application + "\\.)(([a-z]|[0-9]|[-_/])+\\.)*(([a-z]|[0-9]|[-_/])+)$"
	ResourceHelmConfigStrategyRegexp    = "^(resource\\." + _helm + "\\.config\\." + _strategy + "\\.)(([a-z]|[0-9]|[-_/])+\\.)*(([a-z]|[0-9]|[-_/])+)$"

	ResourceHelmInstanceRegexp            = "^(resource\\." + _helm + "\\.instance\\.)(([a-z]|[0-9])+\\.)*(([a-z]|[0-9])+)$"
	ResourceHelmInstanceApplicationRegexp = "^(resource\\." + _helm + "\\.instance\\." + _application + "\\.)(([a-z]|[0-9]|[-_/])+\\.)*(([a-z]|[0-9]|[-_/])+)$"
	ResourceHelmInstanceStrategyRegexp    = "^(resource\\." + _helm + "\\.instance\\." + _strategy + "\\.)(([a-z]|[0-9]|[-_/])+\\.)*(([a-z]|[0-9]|[-_/])+)$"
)

func configRegexpStr(configKind, mod string) (string, error) {
	switch mod {
	case apis2.ModuleDefault:
		switch configKind {
		case apis2.KindApplication:
			return ResourceConfigApplicationRegexp, nil
		case apis2.KindStrategy:
			return ResourceConfigStrategyRegexp, nil
		default:
			return "", fmt.Errorf("unkown kind %v, must in %v", configKind, apis2.ConfigKinds())
		}
	case apis2.ModuleHelm:
		switch configKind {
		case apis2.KindApplication:
			return ResourceHelmConfigApplicationRegexp, nil
		case apis2.KindStrategy:
			return ResourceHelmConfigStrategyRegexp, nil
		default:
			return "", errors.New("unkown type")
		}
	default:
		return "", fmt.Errorf("unkown mode %v must in %v", mod, apis2.Modules())
	}
}

func configRegexpStrWithOutKind(mod string) (string, error) {
	switch mod {
	case apis2.ModuleDefault:
		return ResourceConfigRegexp, nil
		//switch configKind {
		//case apis2.KindApplication:
		//	return ResourceConfigApplicationRegexp, nil
		//case apis2.KindStrategy:
		//	return ResourceConfigStrategyRegexp, nil
		//default:
		//	return "", fmt.Errorf("unkown kind %v, must in %v", configKind, apis2.ConfigKinds())
		//}
	case apis2.ModuleHelm:
		return ResourceHelmConfigRegexp, nil
		//switch configKind {
		//case apis2.KindApplication:
		//	return ResourceHelmConfigApplicationRegexp, nil
		//case apis2.KindStrategy:
		//	return ResourceHelmConfigStrategyRegexp, nil
		//default:
		//	return "", errors.New("unkown type")
		//}
	default:
		return "", fmt.Errorf("unkown mode %v must in %v", mod, apis2.Modules())
	}
}

func instanceRegexpStr(configKind, mod string) (string, error) {
	switch mod {
	case apis2.ModuleDefault:
		switch configKind {
		case apis2.KindApplication:
			return ResourceInstanceApplicationRegexp, nil
		case apis2.KindStrategy:
			return ResourceInstanceStrategyRegexp, nil
		default:
			return "", errors.New("unkown type")
		}
	case apis2.ModuleHelm:
		switch configKind {
		case apis2.KindApplication:
			return ResourceHelmInstanceApplicationRegexp, nil
		case apis2.KindStrategy:
			return ResourceHelmInstanceStrategyRegexp, nil
		default:
			return "", errors.New("unkown type")
		}
	default:
		return "", errors.New("unkown mode")
	}
}
